﻿#region Using Statements
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic;
using Microsoft.Xna.Framework.Input;
using System;
using RhinoXNA.RhinoGraphics.Rhino2D.RhinoHUD;
using RhinoXNA.RhinoGraphics.Rhino2D;
#endregion

namespace RhinoXNA
{
    public struct KeyBinding
    {
        public KeyboardState boardState;
        public MouseState mouseState;
    }

    public abstract class ILevel
    {
        protected List<IRhinoGraphicsComponent> _graphicsComp;
        protected List<Character> _characters;
        protected RhinoHUDManager _hud;

        protected List<Projectile> _projectiles;
        protected List<Projectile> _removableProjectiles;

        public List<Projectile> Projectiles
        {
            get { return _projectiles; }
        }


        public List<Enemy> Enemies
        {
            get
            {
                List<Enemy> enemies = new List<Enemy>();

                foreach (Character c in _characters)
                {
                    if (c is Enemy)
                    {
                        enemies.Add((Enemy)c);
                    }
                }

                return enemies;
            }
        }

        protected KeyBinding _keyBinding;

        public ILevel()
        {
            _graphicsComp = new List<IRhinoGraphicsComponent>();
            _characters = new List<Character>();

            _projectiles = new List<Projectile>();
            _removableProjectiles = new List<Projectile>();

            _hud = new RhinoHUDManager();
        }


        public virtual void Update(GameTime gameTime)
        {
            List<Enemy> deadEnemies = new List<Enemy>();

            foreach (Projectile m in _removableProjectiles)
            {
                _projectiles.Remove(m);
            }

            foreach (Projectile p in _projectiles)
            {
                p.Update(gameTime);
            }

            foreach (Enemy e in this.Enemies)
            {
                e.Update(gameTime);
                if(e.IsDead) {
                    deadEnemies.Add(e);
                }
            }

            foreach (Enemy e in deadEnemies)
            {
                _characters.Remove(e);
            }

            foreach (IRhinoGraphicsComponent comp in _graphicsComp)
            {
                comp.Update(gameTime);
            }

            _hud.Update(gameTime);
        }

        public void RequestProjectileRemoval(Projectile p)
        {
            _removableProjectiles = _projectiles.FindAll(delegate(Projectile dp) { return dp == p; });
        }

        public virtual void Render()
        {
            foreach (IRhinoGraphicsComponent comp in _graphicsComp)
            {
                comp.Render();
            }

            foreach (IRhinoDrawable d in _hud)
            {
                d.Render();
            }
        }
    }
}
